//
//  MyAzkarViewController.m
//  Azkar
//
//  Created by CPX on 2018/9/18.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MyAzkarViewController.h"
#import "MyAzkarCell.h"

/**
 我的祈福页面
 @author yzl
 */
@interface MyAzkarViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSMutableArray *list;
@property (nonatomic, strong) UIImageView *emptyView;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation MyAzkarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initViews{
    self.view.backgroundColor = UIColorHex(#F6F6F6);
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, kScreenWidth, kScreenHeight-NavHeight-TabbarHeight)];
    self.table.estimatedRowHeight = 157;
    self.table.backgroundColor = UIColorHex(#F6F6F6);
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:self.table];
    
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    MJRefreshNormalHeader *he = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
    he.lastUpdatedTimeLabel.hidden = YES;
    he.stateLabel.hidden = YES;
    self.table.mj_header = he;
    self.emptyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_content_img"]];
    [self.view addSubview:self.emptyView];
    self.emptyView.center = CGPointMake(self.view.width/2, self.view.height/2);
}

- (void)initNavigation{
    [self initDefaultNavigation];
    [self.myNavigationBar.backButton setTitle:TLString(@"") forState:UIControlStateNormal];
}

- (void)loadData{
    self.list = [NSMutableArray array];
    [self.table.mj_header beginRefreshing];
}

- (void)reloadData{
    [AzkarApi getMyAzkarListWithLimit:10 offset:0 success:^(id response) {
        [self.list removeAllObjects];
        [self.list addObjectsFromArray:[NSArray yy_modelArrayWithClass:[BlessModel class] json:response]];
        [self.table reloadData];
        if (!self.list.count) {
            self.table.mj_footer = nil;
            self.emptyView.hidden = NO;
        }
        else{
            self.table.mj_footer = self.footer;
            self.emptyView.hidden = YES;
        }
        [self.table.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        if (!self.list.count) {
            self.table.mj_footer = nil;
        }
        [self showHUDInView:error.userInfo[@"msg"]];
        [self.table.mj_header endRefreshing];

    }];
}

- (void)loadMoreData{
    [AzkarApi getMyAzkarListWithLimit:10 offset:self.list.count success:^(id response) {
        NSArray *a = [NSArray yy_modelArrayWithClass:[BlessModel class] json:response];
        if (a.count >= 10) {
            [self.table.mj_footer endRefreshing];
        }
        else{
            [self.table.mj_footer endRefreshingWithNoMoreData];
        }
        [self.list addObjectsFromArray:a];
        if (!self.list.count) {
            self.table.mj_footer = nil;
        }
        else{
            self.table.mj_footer = self.footer;
        }
        [self.table reloadData];
    } failure:^(NSError *error) {
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
    }];
}

#pragma mark - table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.list.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAzkarCell *cell = [MyAzkarCell cellWithTable:tableView];
    cell.blessModel = self.list[indexPath.row];
    return cell;
}

@end
